use bevy_wasm_api::{ApiResult, EntityId};
use serde_json::json;

#[cfg(test)]
mod entity_id_tests {
    use super::*;

    #[test]
    fn test_entity_id_creation() {
        let entity_id = EntityId::new(42);
        assert_eq!(entity_id.raw(), 42);
    }

    #[test]
    fn test_entity_id_from_u32() {
        let entity_id: EntityId = 123u32.into();
        assert_eq!(entity_id.raw(), 123);
    }

    #[test]
    fn test_entity_id_to_string() {
        let entity_id = EntityId::new(456);
        assert_eq!(entity_id.to_string(), "EntityId(456)");
    }

    #[test]
    fn test_entity_id_equality() {
        let entity1 = EntityId::new(100);
        let entity2 = EntityId::new(100);
        let entity3 = EntityId::new(200);

        assert_eq!(entity1, entity2);
        assert_ne!(entity1, entity3);
    }

    #[test]
    fn test_entity_id_hash() {
        use std::collections::HashMap;

        let mut map = HashMap::new();
        let entity_id = EntityId::new(789);

        map.insert(entity_id, "test_value");
        assert_eq!(map.get(&entity_id), Some(&"test_value"));
    }
}

#[cfg(test)]
mod api_result_tests {
    use super::*;

    #[test]
    fn test_api_result_success() {
        let result = ApiResult::ok("success_data");
        assert!(result.success());
        assert!(result.error().is_none());
    }

    #[test]
    fn test_api_result_error() {
        let result = ApiResult::err("error_message".to_string());
        assert!(!result.success());
        assert_eq!(result.error(), Some("error_message".to_string()));
    }

    #[test]
    fn test_api_result_empty() {
        let result = ApiResult::ok_empty();
        assert!(result.success());
        assert!(result.error().is_none());
    }

    #[test]
    fn test_api_result_from_ok() {
        let rust_result: Result<i32, String> = Ok(42);
        let api_result = ApiResult::from(rust_result);

        assert!(api_result.success());
        assert!(api_result.error().is_none());
    }

    #[test]
    fn test_api_result_from_err() {
        let rust_result: Result<i32, String> = Err("test_error".to_string());
        let api_result = ApiResult::from(rust_result);

        assert!(!api_result.success());
        assert_eq!(api_result.error(), Some("test_error".to_string()));
    }

    #[test]
    fn test_api_result_with_complex_data() {
        #[derive(serde::Serialize)]
        struct TestData {
            id: u32,
            name: String,
        }

        let data = TestData {
            id: 123,
            name: "test".to_string(),
        };

        let result = ApiResult::ok(data);
        assert!(result.success());
        assert!(result.error().is_none());
    }
}

#[cfg(test)]
mod error_scenarios_tests {
    use super::*;

    #[test]
    fn test_entity_not_found_error() {
        let entity_id = EntityId::new(999);
        let error_msg = format!("Entity {:?} not found", entity_id);
        let result = ApiResult::err(error_msg.clone());

        assert!(!result.success());
        assert_eq!(result.error(), Some(error_msg));
    }

    #[test]
    fn test_component_missing_error() {
        let entity_id = EntityId::new(123);
        let error_msg = format!(
            "Entity {} has no Transform component",
            entity_id.to_string()
        );
        let result = ApiResult::err(error_msg.clone());

        assert!(!result.success());
        assert_eq!(result.error(), Some(error_msg));
    }
}
